//
//  ILGPay.m
//  ILGDemo
//
//  Created by HappyWork on 2019/2/14.
//  Copyright © 2019 Happy. All rights reserved.
//

#import "ILGPay.h"

#import "ILGApplePay.h"

@interface ILGPay () <ILGApplePayDelegate>
@property (nonatomic, strong) ILGApplePay *applePay;
@end

@implementation ILGPay

- (void)iap_pay:(NSString *)productId orderId:(NSString *)orderId {
    [self.applePay pay:productId orderId:orderId];
}

- (void)iap_addTransactionObserverForInAppPurchase {
    [self.applePay addTransactionObserver];
}

- (void)iap_configureProductIdentifierArr:(NSArray *)array {
    [self.applePay configureProductIdentifierArr:array];
}

- (void)iap_finishTransaction:(NSString *)orderId {
    [self.applePay finishTransaction:orderId];
}

#pragma mark - delegate
- (void)applePayComplete:(NSDictionary *)result error:(NSError *)error {
    if ([self.delegate respondsToSelector:@selector(chargeComplete:error:)]) {
        [self.delegate chargeComplete:result error:error];
    }
}


#pragma mark - other

- (ILGApplePay *)applePay {
    if (!_applePay) {
        _applePay = [[ILGApplePay alloc] init];
        _applePay.delegate = self;
    }
    return _applePay;
}


@end

/* 向苹果服务器验证票据
 NSError *error;
 NSData *receiptData = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
 NSString *receipt = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
 NSDictionary *requestContents = @{@"receipt-data": receipt};
 NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
 options:0
 error:&error];
 if (!requestData) {
 return;
 }
 // Create a POST request with the receipt data.
 NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];//根据是否是沙盒支付验证取正确的地址
 NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
 [storeRequest setHTTPMethod:@"POST"];
 [storeRequest setHTTPBody:requestData];
 // Make a connection to the iTunes Store on a background queue.
 NSOperationQueue *queue = [[NSOperationQueue alloc] init];
 [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
 completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
 if (connectionError) {
} else {
    NSError *error;
    NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data   options:NSJSONReadingAllowFragments error:&error];
    if (!jsonResponse) {  } else {
        NSLog(@"testPay 验证通过：%@", jsonResponse);
    }
}
}];
 */
